package com.zsl.picture.controller;


import com.alibaba.druid.util.StringUtils;
import com.zsl.picture.model.Messages;
import com.zsl.picture.service.MessagesService;
import com.zsl.picture.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 留言表 前端控制器
 * </p>
 *
 * @author zsl
 * @since 2022-03-03
 */
@RestController
@RequestMapping("/messages")
public class MessagesController {
    @Autowired
    private MessagesService messagesService;

    @GetMapping("/{pid}")
    public R messages(@PathVariable("pid") Integer pid){
        List<Messages> list = messagesService.queryMessages(pid);
        return R.ok().put("list",list);
    }

    @PostMapping
    public R add(@RequestBody Messages messages){
        if(StringUtils.isEmpty(messages.getMessage().trim())){
            return R.error("请填写留言内容");
        }
        if(messages.getPId()==null||messages.getPId()==0){
            return R.error("照片ID不存在");
        }
        messages.setCreateTime(new Date());
        messagesService.save(messages);
        return R.ok("留言成功");
    }

}
